package com.bmob.lover.ui.fragment

import android.content.Context
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import androidx.viewpager.widget.ViewPager
import com.qmuiteam.qmui.arch.QMUIFragment
import com.qmuiteam.qmui.util.QMUIResHelper
import com.theone.common.ext.dp2px
import com.theone.common.ext.getColor
import com.theone.common.ext.matchParent
import com.theone.common.ext.setMargin
import com.bmob.lover.R
import com.bmob.lover.ui.fragment.album.AlbumFragment
import com.bmob.lover.ui.fragment.lover.Lover2Fragment
import com.bmob.lover.ui.fragment.lover.LoverPublishFragment
import com.bmob.lover.ui.fragment.lover.LoverReviewFragment
import com.bmob.lover.viewmodel.EventViewModel
import com.qmuiteam.qmui.widget.QMUITopBarLayout
import com.theone.mvvm.base.viewmodel.BaseViewModel
import com.theone.mvvm.core.base.fragment.BaseTabInTitleFragment
import com.theone.mvvm.core.app.ext.init
import com.theone.mvvm.core.app.ext.qmui.addTab
import com.theone.mvvm.core.data.entity.QMUIItemBean
import com.theone.mvvm.ext.getAppViewModel
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-19 13:52
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class Index2Fragment : BaseTabInTitleFragment<BaseViewModel>() {

    private val mEventVm: EventViewModel by lazy { getAppViewModel<EventViewModel>() }

    private var mSelectIndex = 0

    override fun translucentFull(): Boolean = true



    override fun QMUITopBarLayout.initTopBar() {
        addLeftImageButton(
            R.drawable.svg_index_menu,
            R.id.topbar_left_button
        ).setOnClickListener {
            mEventVm.requestToOpenOrCloseDrawer(true)
        }
        addRightImageButton(R.drawable.icon_add_light, R.id.topbar_search).apply {
            setOnClickListener {
                startFragment(
                    LoverPublishFragment()
                )
            }
        }
        setBackgroundAlpha(0)
        topBar.layoutParams = FrameLayout.LayoutParams(matchParent, dp2px(36))
    }

    override fun initTabAndFragments(
        tabs: MutableList<QMUIItemBean>,
        fragments: MutableList<QMUIFragment>
    ) {

        with(tabs) {
            addTab("动态")
            addTab("相册")
        }

        with(fragments) {
            add(Lover2Fragment())
            add(AlbumFragment())
        }

    }


    override fun initView(root: View) {
        super.initView(root)
        getViewPager().addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                mSelectIndex = position
            }

        })
    }

    override fun getNavTitleView(
        context: Context,
        index: Int,
        tabs: MutableList<QMUIItemBean>
    ): IPagerTitleView = SimplePagerTitleView(context).apply {
        text = tabs[index].title
        setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            QMUIResHelper.getAttrDimen(
                context,
                R.attr.app_skin_tab_indicator_text_size
            ).toFloat()
        )
        getColor(R.color.white).let {
            selectedColor = it
            normalColor = it
        }
        paint.isFakeBoldText = true
        setOnClickListener {
            if (index == 0 && mSelectIndex == index) {
                startFragment(LoverReviewFragment())
            } else {
                getViewPager().currentItem = index
            }
        }

    }

    override fun getNavIndicator(context: Context): IPagerIndicator =
        LinePagerIndicator(context).apply {
            setColors(getColor(R.color.white))
            init()
        }

}